<!--
 * @Author: Dorange.Wang
 * @Date: 2021-04-07 16:19:40
 * @LastEditors: your name
 * @LastEditTime: 2021-04-21 19:26:17
 * @Description: file content
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="600px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{ $t("sys.select_role") }}</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>

    <el-form
      class="pl30 pr30"
      :model="formData"
      ref="form"
      label-width="120px"
      label-position="right"
    >
      <el-form-item
        :label="$t('sys.select_role')"
        :rules="[
          {
            required: true,
            message: $t('sys.role_not_null'),
            trigger: 'change',
          },
        ]"
        prop="roleIds"
      >
        <el-select
          v-model="formData.roleIds"
          multiple
          filterable
          style="width: 400px"
        >
          <el-option
            v-for="item in validRoleList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">{{ $t("sys.cancel") }}</el-button>
      <el-button type="primary" :loading="loading" @click="submit">{{
        $t("sys.confirm")
      }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { postHmusergrouprole } from "@/apis/paas/HmUserGroupRoleController.js";
import { getHmrolePageList } from "@/apis/paas/HmRoleController.js";

export default {
  data() {
    return {
      loading: false,
      showVisible: false,
      roleList: [],
      formData: {},
      groupRoles: [],
    };
  },

  computed: {
    validRoleList() {
      return this.roleList.filter(
        (item) =>
          !(this.groupRoles || []).includes(item.id) && item.status === 1
      );
    },
  },

  created() {
    this.loadRoleList();
  },

  watch: {
    showVisible(value) {
      if (value) {
        this.loading = false;
      }
    },
  },

  methods: {
    addForm(record = {}, options = {}) {
      this.showVisible = true;
      this.formData = { ...record };
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
      this.groupRoles = options.groupRoles;
    },

    loadRoleList() {
      getHmrolePageList({
        pageSize: 100000,
        pageNo: 1,
      }).then((res) => {
        this.roleList = res.data;
      });
    },

    async submit() {
      this.loading = true;
      try {
        await this.$refs.form.validate();
        const { groupId, roleIds } = this.formData;
        const params = { groupId, roleIds };
        const request = postHmusergrouprole;
        request(params).then((res) => {
          this.showVisible = false;
          this.$emit("ok");
        });
      } catch (error) {
        this.loading = false;
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
